<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
			</view>
			<view class="uni-group">
				<input class="uni-search" type="text" placeholder="请输入搜索内容" />
				<button class="uni-button" type="default" size="mini">搜索</button>
			</view>
		</view>
		<view class="uni-container">
			<uni-table ref="table" emptyText='没有更多数据' border stripe>
				<uni-tr>
					<uni-th align="center"></uni-th>
					<uni-th align="center"></uni-th>
					<uni-th align="center"></uni-th>
					<uni-th align="center" colspan="4">已参与</uni-th>
					<uni-th align="center" colspan="5">未参与</uni-th>
					<uni-th align="center" colspan="3">已申请</uni-th>
					<uni-th align="center" colspan="3">总数</uni-th>
				</uni-tr>
				<uni-tr>
					<uni-th align="center">序号</uni-th>
					<uni-th align="center">活动名称</uni-th>
					<uni-th align="center">社区名称</uni-th>
					<uni-th align="center">已参与</uni-th>
					<uni-th align="center">未参与</uni-th>
					<uni-th align="center">已申请</uni-th>
					<uni-th align="center">总数</uni-th>
					<uni-th align="center">线上预约</uni-th>
					<uni-th align="center">预约未到</uni-th>
					<uni-th align="center">现场报名</uni-th>
					<uni-th align="center">系统录入</uni-th>
					<uni-th align="center">参与率</uni-th>
					<uni-th align="center">去重人数</uni-th>
					<uni-th align="center">社区人数</uni-th>
					<uni-th align="center">覆盖率</uni-th>
					<uni-th align="center">评价总数</uni-th>
					<uni-th align="center">满意数目</uni-th>
					<uni-th align="center">满意率</uni-th>
				</uni-tr>
				<uni-tr v-for="(item,index) in table_list" :key="index">
					<uni-td align="center">{{index+1}}</uni-td>
					<uni-td align="center">{{item.ActivityName}}</uni-td>
					<uni-td align="center">{{item.community_name }}</uni-td>
					<uni-td align="center">{{item.canyu}}</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
					<uni-td align="center">0</uni-td>
				</uni-tr>
			</uni-table>
		</view>
	</view>
</template>
<script>
	const db = uniCloud.database();
	const abu_activity_db = db.collection("abu_activity");
	const abu_community_db = db.collection("abu_community");
	const abu_baoming_db = db.collection("abu_baoming");

	export default {
		data() {
			return {
				table_list: [],
				c_name: []
			};
		},
		async onLoad() {
			try {
				const [communityData, baomingData] = await Promise.all([
					this.load_community_name(),
					this.loadBaomingData()
				]);
				this.c_name = communityData;
				await this.loadActivityData(baomingData);
			} catch (error) {
				console.error("Failed to load data:", error);
			}
		},
		methods: {
			async load_community_name() {
				const res_c = await abu_community_db.get();
				return res_c.result.data;
			},
			async loadActivityData(baomingData) {
				const activityResult = await abu_activity_db.get();
				if (activityResult.result.data) {
					activityResult.result.data.forEach(activity => {
						const communityCountMap = new Map();
						baomingData.forEach(baoming => {
							if (baoming.ActivityName === activity._id) {
								if (!communityCountMap.has(baoming.community_id)) {
									communityCountMap.set(baoming.community_id, 0);
								}
								communityCountMap.set(baoming.community_id, communityCountMap.get(
									baoming.community_id) + 1);
							}
						});
						communityCountMap.forEach((value, key) => {
							const activity_table = {
								ActivityName: activity.ActivityName,
								community_name: this.get_c_name(key) || '666',
								canyu: value || 0
							};
							this.table_list.push(activity_table);
						});
					});
				}
			},
			async loadBaomingData() {
				const baomingResult = await abu_baoming_db.get();
				return baomingResult.result.data || [];
			},
			get_c_name(id) {
				const community = this.c_name.find(item => item._id === id);
				return community ? community.name : '666';
			}
		}
	};
</script>



<style>

</style>